package cn.sccl.dirms.topo.discovery.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.topo.discovery.model.DiscoverRule;
import cn.sccl.dirms.topo.discovery.model.DiscoverRuleExtends;
import cn.sccl.dirms.topo.discovery.service.DiscoverRuleManager;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 自动发现规则 的Controller
* Created by _minus on 2017-09-22.
*/
@RestController
@RequestMapping(value = "/nms/topo/discovery/discoverRule")
public class DiscoverRuleController extends BaseController {

    @Autowired
    private DiscoverRuleManager discoverRuleManager;

    /**
    * 显示所有数据
    */
    @RequestMapping(value = "/all" , method = RequestMethod.GET)
    public List<DiscoverRule> listAllDiscoverRule(){
        return discoverRuleManager.listAll();
    }

    /**
    * 分页查询，分页参数放置在url路径中
    * @param pageIndex 页码
    * @param pageSize 分页大小
    * @param request
    * @return
    */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}" , method = RequestMethod.GET)
    public PageResult<DiscoverRule> pageQueryDiscoverRule(@PathVariable("pageIndex") long pageIndex , @PathVariable("pageSize") int pageSize , HttpServletRequest request){
        return discoverRuleManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
    * 分页查询，分页参数放置在request中
    * @param request
    * @return
    */
    @RequestMapping(value = "/page" , method = RequestMethod.GET )
    public PageResult<DiscoverRule> pageQueryDiscoverRule(HttpServletRequest request){
        return discoverRuleManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }

    /**
    * 保存数据
    * @param discoverRule
    * @return
    */
    @RequestMapping(value = "/" , method = RequestMethod.POST)
    public DiscoverRule saveDiscoverRule(@RequestBody DiscoverRule discoverRule){
        discoverRuleManager.save(discoverRule);
        return discoverRule;
    }

    //  2017/9/27 根据task id 获取到所有的规则列表

    @RequestMapping(value = "/taskid/{id}" , method = RequestMethod.GET)
    public List<DiscoverRuleExtends> queryDiscoverRuleExtendsByTaskId(@PathVariable("id") String id){
        return discoverRuleManager.queryDiscoverRuleExtendsByTaskId(id);
    }

    //  2017/9/27  根据task id 查询当前任务 不存在的规则

    /**
     * 可用的规则模板选择
     * @param id
     * @return
     */
    @RequestMapping(value = "/taskid/{id}/available" , method = RequestMethod.POST)
    public List<DiscoverRule> queryAvailableDiscoverRuleByTaskId(@PathVariable("id") String id){
        return discoverRuleManager.queryAvailableDiscoverRuleByTaskId(id);
    }

    /**
    * 根据ID删除数据
    * @param id
    */
    @RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteDiscoverRule(@PathVariable("id") String id){
        discoverRuleManager.delete(id);
    }
}
